/*
 * Copyright ©2017 贾维程. All rights reserved.
 */

package org.handover.controller.handover;

import org.handover.domain.Handover;
import org.handover.domain.Shift;
import org.handover.domain.User;
import org.handover.repository.DetailsRepository;
import org.handover.repository.HandoverRepository;
import org.handover.repository.ShiftRepository;
import org.handover.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.persistence.criteria.Predicate;
import java.util.*;

import static org.springframework.data.domain.Sort.*;

@Controller
@RequestMapping("/handover/")
public class HandoverListController {

    @Autowired
    HandoverRepository handoverRepository;
    @Autowired
    UserRepository userRepository;
    @Autowired
    DetailsRepository detailsRepository;
    @Autowired
    ShiftRepository shiftRepository;

    @RequestMapping("list")
    public String list(Model model){
        model.addAttribute("shiftList", shiftRepository.findAll());
        model.addAttribute("userList", userRepository.findAll());
        return "handover/list";
    }

    @RequestMapping("list/ajax")
    @ResponseBody
    public Page<Handover> listAjax(@CookieValue(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                    @CookieValue(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                    @CookieValue(required = false) Long shift,
                                    @CookieValue(required = false) Long user,
                                    @CookieValue(required = false, defaultValue = "1") Integer page,
                                    @CookieValue(required = false, defaultValue = "10") Integer pageSize,
                                    @CookieValue(required = false) String detail)
    {
        Specification<Handover> specification = (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (null != user) {
                list.add(cb.equal(root.get("userId").as(User.class), userRepository.getOne(user)));
            }
            if (null != shift) {
                list.add(cb.equal(root.get("shiftId").as(Shift.class), shiftRepository.getOne(shift)));
            }
            if (null != beginDate) {
                list.add(cb.greaterThanOrEqualTo(root.get("date").as(Date.class), beginDate));
            }
            if (null != endDate) {
                list.add(cb.lessThanOrEqualTo(root.get("date").as(Date.class), endDate));
            }
            if (null != detail) {
                list.add(cb.like(root.join("detailsList").get("detail").as(String.class), "%"+ detail +"%"));
            }
            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        };
        List<Order> orders=new ArrayList<>();
        orders.add(new Order(Direction.DESC, "date"));
        orders.add(new Order(Direction.ASC, "shiftId"));
        return handoverRepository.findAll(specification, PageRequest.of(page-1, pageSize, by(orders)));
    }
}
